LL_GPIO_Init and LL_GPIO_DeInit APIs migration

The LL initialization and de-initialization functions previously available in the stm32tnxx_ll_gpio.c files within HAL1 are no longer present in HAL2. In HAL2, all LL C files ( stm32tnxx_ll_gpio.c) have been removed, leaving only the LL header files ( stm32tnxx_ll_gpio.h) that contain static inline functions.

If users need to initialize or de-initialize GPIO using the LL layer, they have two methods:

  • Utilize STM32CubeMX2 Code Generation: Rely on STM32CubeMX2 to generate code with the LL layer for GPIO initialization and de-initialization.

  • Replace manually the initialization and de-initialization functionsReplace the LL_GPIO_Init and LL_GPIO_DeInit functions

    manually with equivalent sequences using the static inline functions provided in the stm32tnxx_ll_gpio.h file, tailored to their specific configuration and requirements.

This approach ensures that the initialization and de-initialization sequences are precisely addressing the given use case based on the LL static inline functions provided in the stm32tnxx_ll_gpio.h header file. The sequences offer improved footprint optimization compared to the previous LL initialization functions found in stm32tnxx_ll_gpio.c.

Related concept: Remove stm32tnxx_ll_ppp.c files

Method 1: Utilize STM32CubeMX2 Code Generation

LL GPIO configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

LL GPIO main features configuration

Configure the GPIO main features.

GPIO basic feature configuration

Generated GPIO initialization sequence

The GPIO initialization and de-initialization sequences are generated in the mx_gpio_default_init and mx_gpio_default_deinit functions in mx_gpio_default.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

STM32CubeMX2 Generated : GPIO initialization sequence

Topic

HAL1 Code Snippet

HAL2 Code Snippet

GPIO init and config sequence.

void MX_GPIO_Init(void)
{

  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);

  /**/
  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}
system_status_t mx_gpio_default_init(void)
{
  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);

  LL_GPIO_SetPinPull(PA0_GPIO_PORT, PA0_PIN, LL_GPIO_PULL_NO);

  LL_GPIO_SetPinMode(PA0_GPIO_PORT, PA0_PIN, LL_GPIO_MODE_INPUT);

#if defined(USE_HAL_GPIO_HSLV) && (USE_HAL_GPIO_HSLV == 1U)
  LL_GPIO_DisableHighSpeedLowVoltage(PA0_GPIO_PORT, PA0_PIN);
#endif /* USE_HAL_GPIO_HSLV */

  return SYSTEM_OK;
}

GPIO deinit sequence.

void MX_GPIO_DeInit(void)
{

  LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOA);
  LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOA);

}
system_status_t mx_gpio_default_deinit(void)
{

  LL_GPIO_SetPinMode(GPIOA, PA0_PIN, LL_GPIO_MODE_ANALOG);

  LL_GPIO_SetAFPin_0_7(GPIOA, PA0_PIN, LL_GPIO_AF_0);

  LL_GPIO_SetPinSpeed(GPIOA, PA0_PIN, LL_GPIO_SPEED_FREQ_LOW);

  LL_GPIO_SetPinOutputType(GPIOA, PA0_PIN, LL_GPIO_OUTPUT_PUSHPULL);

  LL_GPIO_SetPinPull(GPIOA, PA0_PIN, LL_GPIO_PULL_NO);

  LL_GPIO_ResetOutputPin(GPIOA, PA0_PIN);

  return SYSTEM_OK;
}

Method 2: Replace manually the initialization and de-initialization functions

LL_GPIO_Init() API migration

According to the use-case, implement the precise sequence call based on the LL static inline functions provided in the stm32tnxx_ll_gpio.h header file.

GPIO Mode Comparison between HAL1 and HAL2

HAL1

HAL2

Case of GPIO mode is Input

LL_GPIO_InitTypeDef gpio_initStruct;

GPIO_InitStruct->Pin        = LL_GPIO_PIN_1 |
                              LL_GPIO_PIN_3 |
                              LL_GPIO_PIN_7;
GPIO_InitStruct->Mode       = LL_GPIO_MODE_INPUT;
GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
GPIO_InitStruct->Alternate  = LL_GPIO_AF_0;

LL_GPIO_Init(GPIOB, LL_GPIO_InitTypeDef *GPIO_InitStruct);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_1, LL_GPIO_PULL_NO);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_1, LL_GPIO_MODE_INPUT);

LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_3, LL_GPIO_PULL_NO);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_3, LL_GPIO_MODE_INPUT);

LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_7, LL_GPIO_PULL_NO);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_7, LL_GPIO_MODE_INPUT);

Case of GPIO mode is Analog

LL_GPIO_InitTypeDef gpio_initStruct;

GPIO_InitStruct->Pin        = LL_GPIO_PIN_1 |
                              LL_GPIO_PIN_3 |
                              LL_GPIO_PIN_7;
GPIO_InitStruct->Mode       = LL_GPIO_MODE_ANALOG;
GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
GPIO_InitStruct->Alternate  = LL_GPIO_AF_0;

LL_GPIO_Init(GPIOB, LL_GPIO_InitTypeDef *GPIO_InitStruct);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_1, LL_GPIO_PULL_NO);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_1, LL_GPIO_MODE_ANALOG);

LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_3, LL_GPIO_PULL_NO);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_3, LL_GPIO_MODE_ANALOG);

LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_7, LL_GPIO_PULL_NO);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_7, LL_GPIO_MODE_ANALOG);

Case of GPIO mode is Output

LL_GPIO_InitTypeDef gpio_initStruct;

GPIO_InitStruct->Pin        = LL_GPIO_PIN_1 |
                              LL_GPIO_PIN_3 |
                              LL_GPIO_PIN_7;
GPIO_InitStruct->Mode       = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
GPIO_InitStruct->Alternate  = LL_GPIO_AF_0;

LL_GPIO_Init(GPIOB, LL_GPIO_InitTypeDef *GPIO_InitStruct);
LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_1, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_1, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_1, LL_GPIO_PULL_NO);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_1, LL_GPIO_MODE_OUTPUT);

LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_3, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_3, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_3, LL_GPIO_PULL_NO);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_3, LL_GPIO_MODE_OUTPUT);

LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_7, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_7, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_7, LL_GPIO_PULL_NO);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_7, LL_GPIO_MODE_OUTPUT);

Case of GPIO mode is Alternate Function

LL_GPIO_InitTypeDef gpio_initStruct;

GPIO_InitStruct->Pin        = LL_GPIO_PIN_1 |
                              LL_GPIO_PIN_3 |
                              LL_GPIO_PIN_7;
GPIO_InitStruct->Mode       = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
GPIO_InitStruct->Alternate  = LL_GPIO_AF_4;

LL_GPIO_Init(GPIOB, LL_GPIO_InitTypeDef *GPIO_InitStruct);
LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_1, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_1, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_1, LL_GPIO_PULL_NO);
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_1, LL_GPIO_AF_4);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_1, LL_GPIO_MODE_ALTERNATE);

LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_3, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_3, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_3, LL_GPIO_PULL_NO);
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_3, LL_GPIO_AF_4);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_3, LL_GPIO_MODE_ALTERNATE);

LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_7, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_7, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_7, LL_GPIO_PULL_NO);
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_7, LL_GPIO_AF_4);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_7, LL_GPIO_MODE_ALTERNATE);

LL_GPIO_DeInit() API migration

According to the used GPIO port, Implement the precise calls to LL_AHB2_GRP1_ForceReset() and LL_AHB2_GRP1_ReleaseReset

Example: The application is using GPIO port B

Comparison of HAL1 and HAL2

HAL1

HAL2

LL_GPIO_DeInit(GPIOB);
LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOB);
LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOB);